import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
	  goodsList:[ 
		  { 'id': 1, 'title': '电水壶', 'price': 50.01, src: '/1.jpg' ,'num':1},
		  { 'id': 2, 'title': '电压力锅', 'price': 260.99, src: '/2.jpg','num':1 },
		  { 'id': 3, 'title': '电饭煲', 'price': 200.99, src: '/3.jpg','num':1 },
		  { 'id': 4, 'title': '电磁炉', 'price': 300.99, src: '/4.jpg','num':1},
		  { 'id': 5, 'title': '微波炉', 'price': 400.99, src: '/5.jpg','num':1 },
		  { 'id': 6, 'title': '电饼铛', 'price': 200.99, src: '/6.jpg','num':1 },
		  { 'id': 7, 'title': '豆浆机', 'price': 199.99, src: '/7.jpg','num':1 },
		  { 'id': 8, 'title': '多用途锅', 'price': 510.99, src: '/8.jpg','num':1 }
	],
	shoppingcartList:[
		  { 'id': 1, 'title': '电水壶', 'price': 50.01, src: '/1.jpg' ,'num':1},
	]
  },
  getters: {
	  //reduce
	  totalPrice(state){
		  var total=0
		  state.shoppingcartList.map(item =>{
			  total +=item.price*item.num
		  })
		  return total
	  }
  },
  mutations: {
	  add(state,item){
		let obj=this.state.goodsList.find(it => it.id==item.id)
	    let obj1=state.shoppingcartList.find(it => it.id==item.id)
		   if(obj1){
			 obj1.num++
		   }else{
			  state.shoppingcartList.push(obj)
			  
		  }
	  },
	  del(state,item){
		  //splice
		   state.shoppingcartList.forEach((it,i)=>{
		  			  if(it.id==item.id){
		  				  state.shoppingcartList.splice(i,1)
		  			  }
		  		  })
	  }
  },
  actions: {
  },
  modules: {
  }
})
